package cn.maihe.elg.operation.supports.bank.cmb.util;

import cn.hutool.core.util.StrUtil;
import cn.maihe.elg.operation.supports.bank.cmb.tradingmanager.CashService;
import cn.maihe.elg.operation.supports.bank.cmb.tradingmanager.base.AR;
import cn.maihe.elg.operation.supports.bank.cmb.tradingmanager.pojo.AccountTrade;
import cn.maihe.elg.operation.supports.bank.cmb.tradingmanager.pojo.CashSubAccount;
import cn.maihe.elg.operation.supports.bank.cmb.tradingmanager.pojo.ConfigDTO;
import cn.maihe.elg.operation.supports.bank.cmb.tradingmanager.pojo.NormalAccountTrade;
import cn.maihe.elg.operation.supports.bank.cmb.tradingmanager.pojo.NormalAccountTradeQuery;
import cn.maihe.elg.operation.supports.bank.cmb.tradingmanager.utils.DateUtils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @version V1.0
 * @author: Sidon
 * @createTime: 2021-09-03 16:59
 * Copyright (C)2021-2021 福建省金柠网络科技有限公司. All rights reserved.
 */
@Slf4j
@Component
public class CashServiceUtil {
    private static CashServiceUtil cashServiceUtil;

    //public static final ConfigDTO configDTO;

    @Resource
    private ConfigDTO configDTO;

    @PostConstruct
    public void init() {
        cashServiceUtil = this;
        cashServiceUtil.configDTO = this.configDTO;
    }
/*    static {
        configDTO = new ConfigDTO();
        configDTO.setBankAccount("泉州丰泽得兴融资担保有限公司");
        configDTO.setBankBranchNumber("75");
        configDTO.setBankName("招商银行深圳分行");
        configDTO.setBankNumber("655905254710000");
        configDTO.setPrivateKey("NBtl7WnuUtA2v5FaebEkU0/Jj1IodLGT6lQqwkzmd2E=");
        configDTO.setPublicKey(
                "BNsIe9U0x8IeSe4h/dxUzVEz9pie0hDSfMRINRXc7s1UIXfkExnYECF4QqJ2SnHxLv3z/99gsfDQrQ6dzN5lZj0=");
        configDTO.setSm4Key("VuAzSWQhsoNqzn0K");
        configDTO.setUserId("U004887416");
        //官网新的测试地址-2020-05-11
        configDTO.setUrl("http://cdctest.cmburl.cn/cdcserver/api/v2");
        //Demo中的测试地址
//        configDTO.setUrl("http://183.62.66.60:9080/cdcserver/api/v2");
        //configDTO.setUrl("http://47.117.122.63/cdcserver/api/v2");
    }*/

    /**
     * 2.新增
     * 记账子单元 NTDMAADD
     *
     * @throws Exception
     */
    public static boolean subAccountCreate(String subAccount) {
        //String subAccount = "0000000002";
        //String subAccountName = "泉州丰泽得兴融资担保有限公司";
        final CashSubAccount cashSubAccount = CashSubAccount.builder()
                .bankAccount(cashServiceUtil.configDTO.getBankNumber())
                .subAccount(subAccount)
                .subAccountName(cashServiceUtil.configDTO.getBankAccount())
                .build();
        final AR<JSONObject> subAccountCreate = CashService.subAccountCreate(cashSubAccount, cashServiceUtil.configDTO);
        if (subAccountCreate.isSuccess()) {
            return true;
        } else {
            return false;
        }
    }

    public static String subAccountCreateResult(String subAccount) {
        //String subAccount = "0000000002";
        //String subAccountName = "泉州丰泽得兴融资担保有限公司";
        final CashSubAccount cashSubAccount = CashSubAccount.builder()
                .bankAccount(cashServiceUtil.configDTO.getBankNumber())
                .subAccount(subAccount)
                .subAccountName(cashServiceUtil.configDTO.getBankAccount())
                .build();
        return CashService.subAccountCreateResult(cashSubAccount, cashServiceUtil.configDTO);
    }

    /**
     * 3.修改
     * 记账子单元NTDMAMNT
     *
     * @throws Exception
     */
    public static void subAccountModifyTest() {
        String subAccount = "0000000004";
        String subAccountName = "泉州丰泽得兴融资担保有限公司";
        final CashSubAccount cashSubAccount = CashSubAccount.builder()
                .bankBranchNumber(cashServiceUtil.configDTO.getBankBranchNumber())
                .bankAccount(cashServiceUtil.configDTO.getBankNumber())
                .subAccount(subAccount)
                .subAccountName(subAccountName)
                .overdraft("N")
                .build();

        final AR<Boolean> paymentResultAR = CashService.subAccountModify(cashSubAccount, cashServiceUtil.configDTO);

        if (paymentResultAR.isSuccess()) {

        } else {
            System.out.println(paymentResultAR.getMsg());
        }
    }

    /**
     * 4.查询
     * 记账子单元信息NTDMALST
     *
     * @throws Exception
     */
    public static JSONObject subAccountQuery(String subAccount) {
        //String subAccount = "0000000003";
        final CashSubAccount cashSubAccount = CashSubAccount.builder()
                .bankAccount(cashServiceUtil.configDTO.getBankNumber())
                .subAccount(subAccount)
                .build();
        return CashService.subAccountQuery(cashSubAccount, cashServiceUtil.configDTO).getData();
    }
    /**
     * 4.查询
     * 记账子单元信息NTDMALST --判断子账户是否关闭
     *
     * @throws Exception
     */
    public static boolean subAccountCloseFlag(String subAccount) {
        //String subAccount = "0000000003";
        final CashSubAccount cashSubAccount = CashSubAccount.builder()
                .bankAccount(cashServiceUtil.configDTO.getBankNumber())
                .subAccount(subAccount)
                .build();
        return CashService.subAccountCloseFlag(cashSubAccount, cashServiceUtil.configDTO);
    }

    /**
     * 5.关闭
     * 1.4关闭记账子单元NTDMADLT
     */
    public static boolean subAccountDeleteFlag(String subAccount) {
        //账号
        String accnbr = cashServiceUtil.configDTO.getBankNumber();
        //记账子单元编号，通过更改这个，进行删除
        //对应字段，virtual_sub_account_no
        //String dmanbr = "1000001488";

        return CashService.subAccountDeleteFlag(accnbr, subAccount, cashServiceUtil.configDTO);
    }

    /******************************* 余额与交易查询 ************************************/


    /**
     * 14.查询记账子单元当天交易NTDMTLST
     *
     * @throws Exception
     */
    public static List<AccountTrade> subAccountTradeToday(String subAccount) {
        //String subAccount = "0000000003";
//        String subAccount = "0000000002";
        final CashSubAccount cashSubAccount = CashSubAccount.builder()
                .bankAccount(cashServiceUtil.configDTO.getBankNumber())
                .subAccount(subAccount)
                .build();
        final AR<List<AccountTrade>> listAR = CashService.subAccountTradeToday(cashSubAccount, cashServiceUtil.configDTO);
        if (listAR.isSuccess()) {
            log.info("Count:" + listAR.getData().size());
            return listAR.getData();
        } else {
            log.info(listAR.getMsg());
            return new ArrayList<>();
        }
    }

    /**
     * 14.查询记账子单元当天交易NTDMTLST
     *
     * @throws Exception
     */
    public static List<AccountTrade> findAllAccountTradeToday(String subAccount) {
        final CashSubAccount cashSubAccount = CashSubAccount.builder()
                .bankAccount(cashServiceUtil.configDTO.getBankNumber())
                .subAccount(subAccount)
                .build();
        return CashService.findAllAccountTradeToday(cashSubAccount, cashServiceUtil.configDTO);
    }

    /**
     * 15.查询记账子单元历史交易NTDMTHLS
     *
     * @throws Exception
     */
    public static void subAccountQueryTradeHistoryTest() {
//        String subAccount = "0000000003";
        String subAccount = "0000000003";
        final CashSubAccount cashSubAccount = CashSubAccount.builder()
                .bankAccount(cashServiceUtil.configDTO.getBankNumber())
                .subAccount(subAccount)
//                .beginDate(BaseDateUtils.getDateTime(BaseDateUtil.DATE_PATTERN_CLEAR))
//                .endDate(BaseDateUtils.getDateTime(BaseDateUtil.DATE_PATTERN_CLEAR))
                .beginDate("20220112")
                .endDate("20220112")
                .build();
        final AR<Boolean> paymentResultAR = CashService.subAccountTradeHistory(cashSubAccount, cashServiceUtil.configDTO);
        System.out.println();
    }


    /**
     * 16.查询记账子单元余额NTDMABAL
     *
     * @throws Exception
     */
    public static void subAccountBalanceTest() {
        String subAccount = "0000000003";
//        String subAccount = "0000000002";
        final CashSubAccount cashSubAccount = CashSubAccount.builder()
                .bankAccount(cashServiceUtil.configDTO.getBankNumber())
                .subAccount(subAccount)
                .build();
        final AR<Boolean> paymentResultAR = CashService.subAccountBalance(cashSubAccount, cashServiceUtil.configDTO);
        System.out.println();
    }


    /**
     * 22.记账子单元内部转账NTDMATRX
     *
     * @throws Exception
     */
    public static void subAccountTransferTest() {
//        String subAccount = "0000000003";
        String subAccount = "0000000003";
        final CashSubAccount cashSubAccount = CashSubAccount.builder()
                .bankAccount(cashServiceUtil.configDTO.getBankNumber())
                .paymentAccount(subAccount)
                .acceptAccount("0000000002")
                .amount("0.1")
                .transRemark("记账子单元内部转账NTDMATRX")
                .build();
        final AR<Boolean> paymentResultAR = CashService.subAccountTransfer(cashSubAccount, cashServiceUtil.configDTO);
        if (paymentResultAR.isSuccess()) {

        } else {
            System.out.println(paymentResultAR.getMsg());
        }
    }

    /**
     * 账户管理-》7.财务查询 trsQryByBreakPoint
     *
     * @throws Exception
     */
    public static void trsQryByBreakPoint() throws Exception {
       /* ConfigDTO configDTO = new ConfigDTO();
        configDTO.setBankAccount("福建硕合才储信息科技有限公司");
        configDTO.setBankName("招商银行广州分行黄埔大道支行");
        configDTO.setBankNumber("120918831010103");
        configDTO.setBankBranchNumber("20");
        configDTO.setPublicKey("BEynMEZOjNpwZIiD9jXtZSGr3Ecpwn7r+m+wtafXHb6VIZTnugfuxhcKASq3hX+KX9JlHODDl9/RDKQv4XLOFak=");
        configDTO.setPrivateKey("Rs9P1e1YJcIQUBgoSRiod3Cs8RvS1CPtLYGDfQ+EGHI=");
        configDTO.setUserId("N085479523");
        configDTO.setSm4Key("o9LH3uwfNeTGvaBC");
        configDTO.setUrl("http://47.117.122.63/prodCdcserver");*/

        ConfigDTO configDTO = new ConfigDTO();
        configDTO.setSm4Key("o9LH3uwfNeTGvaBC");
        configDTO.setBankAccount("莆田市荔城区阳柠网络科技有限公司");
        configDTO.setBankBranchNumber("20");
        configDTO.setBankName("招商银行广州分行黄埔大道支行");
        configDTO.setBankNumber("120921387410301");
        configDTO.setPrivateKey("Rs9P1e1YJcIQUBgoSRiod3Cs8RvS1CPtLYGDfQ+EGHI=");
        configDTO.setPublicKey("BEynMEZOjNpwZIiD9jXtZSGr3Ecpwn7r+m+wtafXHb6VIZTnugfuxhcKASq3hX+KX9JlHODDl9/RDKQv4XLOFak=");
        configDTO.setUserId("N085479523");
        configDTO.setUrl("http://47.117.122.63/prodCdcserver");
        Date now = new Date();
        final NormalAccountTradeQuery normalAccountTradeQuery = NormalAccountTradeQuery.builder()
                .bankAccount(configDTO.getBankNumber())
                .beginDate(DateUtils.format(org.apache.commons.lang3.time.DateUtils.addDays(now, -90), "yyyyMMdd"))
                .endDate(DateUtils.format(now, "yyyyMMdd"))
                .build();
        List<NormalAccountTrade> normalAccountTradeList = CashService.findNormalAccountTradeAll(normalAccountTradeQuery, configDTO);
        if (normalAccountTradeList != null && normalAccountTradeList.size() > 0) {
            normalAccountTradeList = normalAccountTradeList.stream().filter(data -> "C".equals(data.getLoanCode()) && StringUtils.isEmpty(data.getVirtualNbr().trim())).collect(Collectors.toList());
            if (normalAccountTradeList.size() > 0) {
                for (NormalAccountTrade normalAccountTrade : normalAccountTradeList) {
                    String tradeFlowsNumber = normalAccountTrade.getSerialNumber();
                    String acctName = normalAccountTrade.getCtpAcctName();
                    String acctNbr = normalAccountTrade.getCtpAcctNbr();
                    String transAmount = normalAccountTrade.getTransAmount();
                    String tradeTransDate = normalAccountTrade.getTransDate();
                    System.out.println(StrUtil.format("{},公司:{},银行账户:{},往一般户:{}充值:{},流水号:{}", tradeTransDate, acctName, acctNbr, configDTO.getBankAccount(), transAmount, tradeFlowsNumber));
                }
            } else {
                System.out.println("无今日转账记录");
            }
        } else {
            System.out.println("今日无交易记录");
        }


    }
}
